<!--图片预览组件-->
<template>
  <el-image
    :src="`${realSrc}`"
    fit="cover"
    :style="`width:${realWidth};height:${realHeight};`"
    :preview-src-list="realSrcList"
  >
    <div slot="error" class="image-slot">
      <i class="el-icon-picture-outline"></i>
    </div>
  </el-image>
</template>

<script>

export default {
  name: "ImagePreview",
  props: {
    /**
     * 图片地址
     */
    src: {
      type: [Array, String],
      default: ""
    },
    /**
     * 宽
     */
    width: {
      type: [Number, String],
      default: ""
    },
    /**
     * 高
     */
    height: {
      type: [Number, String],
      default: ""
    }
  },
  computed: {
    /**
     * 获取真正的地址
     * @returns {string}
     */
    realSrc() {
      if (!this.src) {
        return "";
      }
      if (Array.isArray(this.src)) {
        return this.src[0];
      }else{
        return this.src;
      }
    },
    /**
     * 获取真正的地址列表
     * @returns {*[]}
     */
    realSrcList() {
      if (!this.src) {
        return [];
      }
      let real_src_list
      if (Array.isArray(this.src)) {
        real_src_list = this.src;
      }else{
        real_src_list = [this.src]
      }
      let srcList = [];
      real_src_list.forEach(item => {
        return srcList.push(item);
      });
      return srcList;
    },
    /**
     * 获取真正宽度
     * @returns {string|string}
     */
    realWidth() {
      return typeof this.width == "string" ? this.width : `${this.width}px`;
    },
    /**
     * 获取真正高度
     * @returns {string|string}
     */
    realHeight() {
      return typeof this.height == "string" ? this.height : `${this.height}px`;
    }
  },
};
</script>

<style lang="scss" scoped>
.el-image {
  border-radius: 5px;
  background-color: #ebeef5;
  box-shadow: 0 0 5px 1px #ccc;
  ::v-deep .el-image__inner {
    transition: all 0.3s;
    cursor: pointer;
    &:hover {
      transform: scale(1.2);
    }
  }
  ::v-deep .image-slot {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    color: #909399;
    font-size: 30px;
  }
}
</style>
